﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MVCQuick.Framework.Logging;
using MVCQuick.Framework.Container;
using MVCQuick.Framework.Repository.NHibernate;
using MvcMusicStore.Controllers;
using System.Collections;
using MVCQuick.Framework.Mvc;
using Mvc3ToolsUpdateWeb_Default.Controllers;
using MVCQuick.Framework;
using MvcMusicStore.Models;
using MVCQuick.Framework.Repository;

namespace MvcMusicStore
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorLogAttribute());            
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            AppDomain.CurrentDomain.SetData("SQLServerCompactEditionUnderWebHosting", true);


            Logger.ConfigureLog4Net(HttpContext.Current.Server.MapPath("~/log"), LogLevel.Info, LogLevel.Info);

            NHibernateManager.Configuration();
            NHibernateManager.AddMapping("MVCQuick",
                typeof(EntityBase).Assembly,
                typeof(Album).Assembly);         
            NHibernateManager.UpdateSchema();

            //Register Repository
            ClassicContainer.Register<NHibernateRepository>("NHibernateRepository");

            ////Register  Controller
            IDictionary properties = new Dictionary<String, Object>();
            properties.Add("Repository", ClassicContainer.GetObject("NHibernateRepository"));
            ClassicContainer.Register<AccountController>("Account", properties);
            ClassicContainer.Register<CheckoutController>("Checkout", properties);
            ClassicContainer.Register<HomeController>("Home", properties);
            ClassicContainer.Register<ShoppingCartController>("ShoppingCart", properties);
            ClassicContainer.Register<StoreController>("Store", properties);
            ClassicContainer.Register<StoreManagerController>("StoreManager", properties);
            ClassicContainer.Register<SampleDataController>("SampleData", properties);

            //Set ControllerFactory
            ControllerBuilder.Current.SetControllerFactory(typeof(ClassicControllerFactory));

            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);    
        }
    }
}